package com.zprmb.udzt;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zprmb.udzt.adapter.Address_adapter;
import com.zprmb.udzt.bean.Address;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;
/**
 * 我的收货地址
 * @author shenshao
 *
 */
public class My_address extends BaseActivity implements OnClickListener{
	private Button btn_add_address;
	private ImageView img_back,img_none;
	private ListView lv;
	private Address_adapter adapter;
	private List<Address> list=new ArrayList<Address>();
	private RelativeLayout rl_back;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_address);
		 BbtApplication.getInstance().addActvity(this);
		btn_add_address=(Button)findViewById(R.id.btn_add_address);
		img_back=(ImageView)findViewById(R.id.img_back);
		lv=(ListView)findViewById(R.id.lv);
		img_none=(ImageView)findViewById(R.id.iv_v4_nonetwork);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		rl_back.setOnClickListener(this);
		img_back.setOnClickListener(this);
		btn_add_address.setOnClickListener(this);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(My_address.this,Local_address.class);
				intent.putExtra("name", list.get(position).getReceiveName());
				intent.putExtra("tel", list.get(position).getReceivePhone());
				intent.putExtra("address", list.get(position).getProvince()+list.get(position).getCity()+list.get(position).getArea());
				intent.putExtra("detail",list.get(position).getExpressAddr());
				intent.putExtra("province",list.get(position).getProvince());
				intent.putExtra("city",list.get(position).getCity());
				intent.putExtra("area",list.get(position).getArea());				
				intent.putExtra("id",list.get(position).getAddressId());
				startActivity(intent);
				
			}
		});
		initData(true);
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initData(false);
	}
	private void initData(boolean flag) {
		// TODO Auto-generated method stub
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				if(jsonObject.optInt("success")==1){
					
					if(!jsonObject.optString("data").equals("")){
						Log.e("my_address", jsonObject.toString());
						Gson gson=new Gson();
						list=gson.fromJson(jsonObject.optString("data"), new TypeToken<ArrayList<Address>>() {
						}.getType());
						adapter=new Address_adapter(list, My_address.this);
						lv.setAdapter(adapter);
						if(list.size()<1){
							lv.setVisibility(View.GONE);
							img_none.setVisibility(View.VISIBLE);
						}else{
							lv.setVisibility(View.VISIBLE);
							img_none.setVisibility(View.GONE);
						}
						
					}
				}else {
					
					if (jsonObject.optString("comment").equals("please login")) {
						
						
						if(getShare().getString("mobile", "").equals("")){
							Intent intent = new Intent(My_address.this,Log_or_RegisActivity.class);
							intent.putExtra("who", 4);
							startActivity(intent);
							
						}else{
							
							Intent intent = new Intent(My_address.this,LoginActivity.class);
							intent.putExtra("who", 4);
							startActivity(intent);
							
						}
						
					}else{
						Toast.makeText(My_address.this, jsonObject.optString("comment"), 2000).show();
						My_address.this.finish();
					}
				}
				
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(flag);
		volleryUtil.execute(My_address.this, Constant.URL+"/api/v5/user/receiveAdr","");
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.btn_add_address:
			Intent intent=new Intent(My_address.this,Add_address.class);
			startActivity(intent);
			break;
		case R.id.img_back:
			finish();
			break;
		case R.id.rl_back:
			finish();
			break;
		}
	}

}
